package com.jiayou.game

import cn.hutool.core.bean.BeanUtil
import cn.hutool.json.JSONUtil
import com.jiayou.game.message.TicTacToeMessage
import com.jiayou.socket.GameSocket
import javax.websocket.Session

/**
@author 李 双
@data   2020-09-06  11:17
@info   玩家数据
 */
/**
 * @param username -> 用户名
 * @param title    -> 昵称
 */
class Player(val username: String, val title: String, val gameSocket: GameSocket) : IPlayer<TicTacToeMessage> {

    //能否改变游戏状态
    var changeState = true

    //游戏房间
    var gameHome: String? = null

    //游戏状态
    var state = GameState.ONLINE

    //是否投降
    var isSurrender = false

    //游戏数据
    var ticTacToeMessage: TicTacToeMessage? = null


    override fun sendMessage(ticTacToeMessage: TicTacToeMessage) {
        gameSocket.session.basicRemote.sendText(JSONUtil.toJsonPrettyStr(ticTacToeMessage))
    }

    override fun surrender() {
        isSurrender = true
    }

    override fun readyGame() {
        state = GameState.READY
    }

    override fun cancleReadyGame() {
        state = GameState.ONLINE
    }

    override fun canChangeState() {
        changeState = true
    }

    override fun cantChangeState() {
        changeState = false
    }

    override fun gameOver() {
        gameHome = null
    }

    override fun toString(): String {
        return "Player(username='$username', title='$title', gameHome=$gameHome, state=$state, ticTacToeMessage=$ticTacToeMessage)"
    }

}